package sddz.foodmanage.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.zhy.autolayout.AutoLinearLayout;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import sddz.foodmanage.R;
import sddz.foodmanage.adapter.RecyclerAdapter;
import sddz.foodmanage.adapter.UniversalAdapter;
import sddz.foodmanage.adapter.UniversalViewHolder;
import sddz.foodmanage.adapter.ViewHolder;
import sddz.foodmanage.base.BaseActivity;
import sddz.foodmanage.model.DoneListBean;
import sddz.foodmanage.view.MyListView;

/**
 * @Date:2019/7/8
 * @Author:lbb 订单详情页面
 * 中队的完成，其他，供应商 完成 ，管理员详情
 */
public class OrderDetailActivity extends BaseActivity {

    @BindView(R.id.title_navigation_icon)
    ImageView titleNavigationIcon;
    @BindView(R.id.title_text)
    TextView titleText;
    @BindView(R.id.title_menu_icon)
    ImageView titleMenuIcon;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.image_title_right)
    ImageView imageTitleRight;
    @BindView(R.id.tv_pingji)
    TextView tvPingji;
    @BindView(R.id.tv_zhongdui)
    TextView tvZhongdui;
    @BindView(R.id.lv_order)
    MyListView lvOrder;
    @BindView(R.id.tv_you)
    TextView tvYou;
    @BindView(R.id.tv_liang)
    TextView tvLiang;
    @BindView(R.id.tv_zhong)
    TextView tvZhong;
    @BindView(R.id.tv_cha)
    TextView tvCha;
    @BindView(R.id.send_gather_rv)
    RecyclerView sendGatherRv;
    @BindView(R.id.all_image)
    AutoLinearLayout allImage;
    @BindView(R.id.image_add)
    ImageView imageAdd;
    @BindView(R.id.all_waitcomment)
    AutoLinearLayout allWaitcomment;
    @BindView(R.id.tv_comment)
    TextView tvComment;
    @BindView(R.id.activity_order_detail)
    AutoLinearLayout activityOrderDetail;
    @BindView(R.id.et_content)
    TextView etContent;
    @BindView(R.id.tv_dinner_num)
    TextView tvDinnerNum;//用餐人数


    private String status;
    private int position;
    private List<DoneListBean.DataBeanX.PaginatorBean.DataBean.GoodsBean> goodslist = new ArrayList<>();
    private UniversalAdapter<DoneListBean.DataBeanX.PaginatorBean.DataBean.GoodsBean> adapter;
    private RecyclerAdapter<String> mAdapter;//图片
    private ArrayList<String> mList = new ArrayList<>();//图片

    //用来标识是否为自采
    private int isSelfbuy = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_detail);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    private void initData() {
        Intent intent = getIntent();
        status = intent.getStringExtra("status");
        position = intent.getIntExtra("position", 1);
        Log.e("positiondetail", position + "");
        etContent.setSelected(false);
        DoneListBean.DataBeanX.PaginatorBean.DataBean DataBean =
                (DoneListBean.DataBeanX.PaginatorBean.DataBean) intent.getSerializableExtra("data");
//        Log.e("DataBean", data);
//       List<DoneListBean.DataBeanX.PaginatorBean.DataBean> DataBean= (List<DoneListBean.DataBeanX.PaginatorBean.DataBean>) new Gson().fromJson(data,DoneListBean.DataBeanX.PaginatorBean.DataBean.class);

        isSelfbuy = DataBean.getShopping_type();
        goodslist.addAll(DataBean.getGoods());
        tvZhongdui.setText(DataBean.getTotal_price() + "元");
        tvPingji.setText(DataBean.getNumber() + "");
        tvDinnerNum.setText(DataBean.getDinner_num()+"");
        if (!TextUtils.isEmpty(DataBean.getComment().getGrade_desc() + "")) {
            DoneListBean.DataBeanX.PaginatorBean.DataBean.CommentBean commentBean = DataBean.getComment();
            Log.e("commentBean", JSON.toJSONString(commentBean));
            if (!TextUtils.isEmpty(commentBean.getGrade_desc())) {
                if (commentBean.getGrade_desc().equals("优")) {
                    tvYou.setBackgroundDrawable(getResources().getDrawable(R.drawable.redcircle));
                    tvLiang.setBackgroundDrawable(getResources().getDrawable(R.drawable.graycircle));
                    tvZhong.setBackgroundDrawable(getResources().getDrawable(R.drawable.graycircle));
                    tvCha.setBackgroundDrawable(getResources().getDrawable(R.drawable.graycircle));
//                       tvYou.setBackground(getDrawable(R.drawable.redcircle));
//                       tvLiang.setBackground(getDrawable(R.drawable.graycircle));
//                       tvZhong.setBackground(getDrawable(R.drawable.graycircle));
//                       tvCha.setBackground(getDrawable(R.drawable.graycircle));
                } else if (commentBean.getGrade_desc().equals("良")) {
                    tvYou.setBackgroundDrawable(getResources().getDrawable(R.drawable.graycircle));
                    tvLiang.setBackgroundDrawable(getResources().getDrawable(R.drawable.redcircle));
                    tvZhong.setBackgroundDrawable(getResources().getDrawable(R.drawable.graycircle));
                    tvCha.setBackgroundDrawable(getResources().getDrawable(R.drawable.graycircle));
//                       tvYou.setBackground(getDrawable(R.drawable.graycircle));
//                       tvLiang.setBackground(getDrawable(R.drawable.redcircle));
//                       tvZhong.setBackground(getDrawable(R.drawable.graycircle));
//                       tvCha.setBackground(getDrawable(R.drawable.graycircle));
                } else if (commentBean.getGrade_desc().equals("中")) {
                    tvYou.setBackgroundDrawable(getResources().getDrawable(R.drawable.graycircle));
                    tvLiang.setBackgroundDrawable(getResources().getDrawable(R.drawable.graycircle));
                    tvZhong.setBackgroundDrawable(getResources().getDrawable(R.drawable.redcircle));
                    tvCha.setBackgroundDrawable(getResources().getDrawable(R.drawable.graycircle));
//                       tvYou.setBackground(getDrawable(R.drawable.graycircle));
//                       tvLiang.setBackground(getDrawable(R.drawable.graycircle));
//                       tvZhong.setBackground(getDrawable(R.drawable.redcircle));
//                       tvCha.setBackground(getDrawable(R.drawable.graycircle));
                } else {
                    tvYou.setBackgroundDrawable(getResources().getDrawable(R.drawable.graycircle));
                    tvLiang.setBackgroundDrawable(getResources().getDrawable(R.drawable.graycircle));
                    tvZhong.setBackgroundDrawable(getResources().getDrawable(R.drawable.graycircle));
                    tvCha.setBackgroundDrawable(getResources().getDrawable(R.drawable.redcircle));
//                       tvYou.setBackground(getDrawable(R.drawable.graycircle));
//                       tvLiang.setBackground(getDrawable(R.drawable.graycircle));
//                       tvZhong.setBackground(getDrawable(R.drawable.graycircle));
//                       tvCha.setBackground(getDrawable(R.drawable.redcircle));
                }
            }

            if (!TextUtils.isEmpty(commentBean.getComment())) {
                etContent.setText(commentBean.getComment());
            } else {
                etContent.setText("无评论");
            }

            if(2==DataBean.getShopping_type()){

            }else{
                for (int i = 0; i < commentBean.getImgs().size(); i++) {
                    mList.add(commentBean.getImgs().get(i).getUrl());
                }
            }
        }

        if(2==DataBean.getShopping_type()){
            allWaitcomment.setVisibility(View.GONE);
            etContent.setVisibility(View.GONE);
            allImage.setVisibility(View.GONE);
        }
//        }
    }

    private void initView() {
        titleText.setText("订单详情");
//        tvComment.setVisibility(View.GONE);
        adapter = new UniversalAdapter<DoneListBean.DataBeanX.PaginatorBean.DataBean.GoodsBean>(mActivity, goodslist, R.layout.item_row_list) {
            @Override
            public void convert(UniversalViewHolder holder, int position, DoneListBean.DataBeanX.PaginatorBean.DataBean.GoodsBean goodsBean) {
                if (status.equals("待配送")) {
                    holder.getView(R.id.tv_sjweight).setVisibility(View.GONE);
                } else {
                    holder.getView(R.id.tv_sjweight).setVisibility(View.VISIBLE);
                }
                holder.setText(R.id.tv_name, goodsBean.getName());
                holder.setText(R.id.tv_unit, goodsBean.getPrice() + "元/" + goodsBean.getUnit());
                holder.setText(R.id.tv_xdweight, goodsBean.getAmount_data() + goodsBean.getUnit());
                holder.setText(R.id.tv_sjweight, goodsBean.getAmount() + goodsBean.getUnit());
                holder.setText(R.id.tv_zongje, goodsBean.getTotal_price() + "元");
                //==================================================================================
                //如果该商品是自采就隐藏备注图标
//                goodslist.get()
                final String remark = goodsBean.getRemark();
                if (remark.isEmpty()) {
                    holder.setImageResource(R.id.iv_note, R.mipmap.icon_remark_gray);
                } else {
                    holder.setImageResource(R.id.iv_note, R.mipmap.icon_remark_red);
                }
                if(2 == isSelfbuy){
                    holder.getView(R.id.iv_note).setVisibility(View.GONE);
                }
                holder.getView(R.id.iv_note).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (!remark.isEmpty()) {
                            final EditText editText = new EditText(OrderDetailActivity.this);
                            editText.setFocusable(false);
                            editText.setText(remark);
                            AlertDialog.Builder inputDialog =
                                    new AlertDialog.Builder(OrderDetailActivity.this);
                            inputDialog.setTitle("备注信息:")
                                    .setPositiveButton("关闭", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialogInterface, int i) {

                                        }
                                    })
                                    .setView(editText)
                                    .show();
                            ;

                        }


                    }
                });
                //==================================================================================
            }

        };
        lvOrder.setAdapter(adapter);
        mAdapter = new RecyclerAdapter<String>(mActivity, mList, R.layout.image_item_layout) {
            @Override
            protected void convert(ViewHolder holder, String s, int position) {
                holder.getView(R.id.iv_delete).setVisibility(View.GONE);
                holder.setImageFile3(mActivity, R.id.iv_item, s);

            }
        };
        sendGatherRv.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
        sendGatherRv.setAdapter(mAdapter);
    }

    @OnClick({R.id.title_navigation_icon, R.id.tv_comment})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_navigation_icon:
                finish();
                break;
            case R.id.tv_comment:
                finish();
                break;
        }

        finish();

    }
}
